package com.net.oil.car.adapter;
/*
 * Created by lch on 2018/3/13 0013.
 */

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.car.bean.CarBrandBean;
import com.net.oil.http.AppUrl;
import com.net.oil.util.StringUtil;
import com.net.oil.util.glide.GlideUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CarBrandRecyclerViewAdapter extends RecyclerView.Adapter<CarBrandRecyclerViewAdapter.ViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private List<CarBrandBean> list;
    private OnCarBrandItemClickListener onCarBrandItemClickListener;

    public CarBrandRecyclerViewAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        list = new ArrayList<>();
    }

    public void updateData(List<CarBrandBean> list) {
        if (this.list != null) {
            this.list.clear();
            this.list.addAll(list);
        } else {
            this.list = new ArrayList<>();
            this.list.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void setOnCarBrandItemClickListener(OnCarBrandItemClickListener onCarBrandItemClickListener) {
        this.onCarBrandItemClickListener = onCarBrandItemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.car_brand_recycler_view_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        final CarBrandBean carBrandBean = list.get(position);
        if (carBrandBean != null){
            String letter = carBrandBean.getInitials();
            if (!StringUtil.isEmpty(letter)){
                holder.letterItem.setText(letter);
            }

            if (!StringUtil.isEmpty(carBrandBean.getName())){
                holder.carName.setText(carBrandBean.getName());
            }

            if (!StringUtil.isEmpty(carBrandBean.getLogo())){
                GlideUtil.getInstance().loadCircleImage(context, holder.carLogo, AppUrl.getImageUrl(carBrandBean.getLogo()), R.drawable.car_logo_err);
            }

            //根据position获取首字母作为目录catalog
            final String catalog = carBrandBean.getInitials();

            //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
            if(position == getPositionForSection(catalog)){
                holder.letterItem.setVisibility(View.VISIBLE);
                holder.letterItem.setText(carBrandBean.getInitials().toUpperCase());
            }else {
                holder.letterItem.setVisibility(View.GONE);
            }

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onCarBrandItemClickListener.onClick(carBrandBean.getId());
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        } else {
            return 0;
        }
    }

    /**
     * 获取catalog首次出现位置
     */
    public int getPositionForSection(String catalog) {
        for (int i = 0; i < getItemCount(); i++) {
            String sortStr = list.get(i).getInitials();
            if (catalog.equalsIgnoreCase(sortStr)) {
                return i;
            }
        }
        return -1;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.car_logo)
        ImageView carLogo;
        @BindView(R.id.car_name)
        TextView carName;
        @BindView(R.id.letter_item)
        TextView letterItem;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnCarBrandItemClickListener{
        void onClick(int carBrandId);
    }
}
